﻿using System;
using System.Diagnostics;
using Teigha.Core;
using Teigha.TD;

namespace YJKCopyModel.InteractiveOperation
{
	public class DwgTextInfo
	{
		public string ItemName
		{
			get
			{
				bool flag = this.m_odDbText == null;
				string result;
				if (flag)
				{
					result = string.Empty;
				}
				else
				{
					bool flag2 = this.m_odDbText is OdDbText;
					if (flag2)
					{
						OdDbText odDbText = this.m_odDbText as OdDbText;
						result = odDbText.textString();
					}
					else
					{
						bool flag3 = this.m_odDbText is OdDbMText;
						if (flag3)
						{
							string text = string.Empty;
							OdDbMText odDbMText = this.m_odDbText as OdDbMText;
							try
							{
								OdRxObjectPtrArray odRxObjectPtrArray = new OdRxObjectPtrArray();
								odDbMText.explodeGeometry(odRxObjectPtrArray);
								foreach (OdRxObject pObj in odRxObjectPtrArray)
								{
									OdDbText odDbText2 = OdDbText.cast(pObj);
									text += odDbText2.textString();
								}
							}
							catch (Exception ex)
							{
								Trace.Assert(true, ex.Message);
							}
							result = text;
						}
						else
						{
							result = string.Empty;
						}
					}
				}
				return result;
			}
		}

		public OdDbEntity Text
		{
			get
			{
				return this.m_odDbText;
			}
		}

		public OdGePoint3d Positon
		{
			get
			{
				return this.m_ptPos;
			}
		}

		public OdGeExtents3d Bounding
		{
			get
			{
				return this.m_geBounding;
			}
		}

		public OdGePoint3d Center
		{
			get
			{
				return this.m_ptContentCenter;
			}
		}

		public OdGePoint3d BoundingCenter
		{
			get
			{
				return this.m_ptBoungdingCenter;
			}
		}

		public OdGePoint3dArray ActualBoungding
		{
			get
			{
				return this.m_ptActualBoungding;
			}
		}

		public double Rotation
		{
			get
			{
				bool flag = this.m_odDbText == null;
				double result;
				if (flag)
				{
					result = 0.0;
				}
				else
				{
					bool flag2 = this.m_odDbText is OdDbText;
					if (flag2)
					{
						OdDbText odDbText = this.m_odDbText as OdDbText;
						result = odDbText.rotation();
					}
					else
					{
						bool flag3 = this.m_odDbText is OdDbMText;
						if (flag3)
						{
							OdDbMText odDbMText = this.m_odDbText as OdDbMText;
							result = odDbMText.rotation();
						}
						else
						{
							result = 0.0;
						}
					}
				}
				return result;
			}
		}

		public double ActualRotation
		{
			get
			{
				return this.m_ActualRotation;
			}
		}

		public double Height
		{
			get
			{
				return this.m_dHeight;
			}
		}

		public double Width
		{
			get
			{
				return this.m_dWidth;
			}
		}

		public double FontRotation
		{
			get
			{
				return this.m_dFontRotation;
			}
		}

		public DwgTextInfo(OdDbText odDbText, OdGeMatrix3d matrix)
		{
			this.m_odDbText = odDbText;
			this.m_matrix = matrix;
			this.m_ptPos = odDbText.position().transformBy(this.m_matrix);
			OdGePoint3dArray odGePoint3dArray = new OdGePoint3dArray();
			odDbText.getBoundingPoints(odGePoint3dArray);
			double num = odGePoint3dArray[0].distanceTo(odGePoint3dArray[2]);
			double num2 = odGePoint3dArray[0].distanceTo(odGePoint3dArray[1]);
			this.m_ptActualBoungding = new OdGePoint3dArray();
			foreach (OdGePoint3d odGePoint3d in odGePoint3dArray)
			{
				OdGePoint3d x = odGePoint3d.transformBy(matrix);
				this.m_ptActualBoungding.Add(x);
			}
			this.m_dHeight = this.m_ptActualBoungding[0].distanceTo(this.m_ptActualBoungding[2]);
			this.m_dWidth = this.m_ptActualBoungding[0].distanceTo(this.m_ptActualBoungding[1]);
			OdGePoint3d point = new OdGePoint3d(odDbText.position().x + num2 / 2.0, odDbText.position().y + num / 2.0, 0.0);
			this.m_ptContentCenter = this.TransToActualPos(point, odDbText.rotation(), odDbText.position());
			this.GetBounding();
			this.GetFontRotation(odDbText.textStyle());
			this.GetActualRotation();
		}

		public DwgTextInfo(OdDbMText odDbMText, OdGeMatrix3d matrix)
		{
			this.m_odDbText = odDbMText;
			this.m_matrix = matrix;
			this.m_ptPos = odDbMText.location().transformBy(this.m_matrix);
			double num = odDbMText.actualHeight();
			double num2 = odDbMText.actualWidth();
			OdGePoint3d mtextActualLeftDownPos = this.GetMTextActualLeftDownPos(odDbMText);
			OdGePoint3d point = new OdGePoint3d(mtextActualLeftDownPos.x, mtextActualLeftDownPos.y + num, 0.0);
			OdGePoint3d odGePoint3d = this.TransToActualPos(point, odDbMText.rotation(), odDbMText.location());
			this.m_ptActualBoungding.Add(odGePoint3d);
			OdGePoint3d point2 = new OdGePoint3d(mtextActualLeftDownPos.x + num2, mtextActualLeftDownPos.y + num, 0.0);
			OdGePoint3d x = this.TransToActualPos(point2, odDbMText.rotation(), odDbMText.location());
			this.m_ptActualBoungding.Add(x);
			OdGePoint3d point3 = new OdGePoint3d(mtextActualLeftDownPos.x, mtextActualLeftDownPos.y, 0.0);
			OdGePoint3d odGePoint3d2 = this.TransToActualPos(point3, odDbMText.rotation(), odDbMText.location());
			this.m_ptActualBoungding.Add(odGePoint3d2);
			this.m_dHeight = odGePoint3d.distanceTo(point3);
			OdGePoint3d point4 = new OdGePoint3d(mtextActualLeftDownPos.x + num2, mtextActualLeftDownPos.y, 0.0);
			OdGePoint3d odGePoint3d3 = this.TransToActualPos(point4, odDbMText.rotation(), odDbMText.location());
			this.m_ptActualBoungding.Add(odGePoint3d3);
			this.m_dWidth = odGePoint3d3.distanceTo(odGePoint3d2);
			OdGePoint3d point5 = new OdGePoint3d(mtextActualLeftDownPos.x + num2 / 2.0, mtextActualLeftDownPos.y + num / 2.0, 0.0);
			this.m_ptContentCenter = this.TransToActualPos(point5, odDbMText.rotation(), odDbMText.location());
			this.GetBounding();
			this.GetFontRotation(odDbMText.textStyle());
			this.GetActualRotation();
		}

		private void GetBounding()
		{
			bool flag = this.m_odDbText != null && this.m_odDbText.getGeomExtents(this.m_geBounding) == OdResult.eOk;
			if (flag)
			{
				this.m_geBounding.transformBy(this.m_matrix);
				OdGePoint3d odGePoint3d = this.m_geBounding.maxPoint();
				OdGePoint3d odGePoint3d2 = this.m_geBounding.minPoint();
				this.m_ptBoungdingCenter = new OdGePoint3d(odGePoint3d2.x + (odGePoint3d.x - odGePoint3d2.x) / 2.0, odGePoint3d2.y + (odGePoint3d.y - odGePoint3d2.y) / 2.0, 0.0);
			}
		}

		private OdGePoint3d TransToActualPos(OdGePoint3d point, double rotition, OdGePoint3d pntBase)
		{
			OdGePoint3d odGePoint3d = new OdGePoint3d(point.x, point.y, 0.0);
			odGePoint3d.rotateBy(rotition, new OdGeVector3d(0.0, 0.0, 1.0), pntBase);
			odGePoint3d.transformBy(this.m_matrix);
			return odGePoint3d;
		}

		private void GetFontRotation(OdDbObjectId odDbObjectId)
		{
			OdDbTextStyleTableRecord odDbTextStyleTableRecord = odDbObjectId.safeOpenObject(OpenMode.kForRead) as OdDbTextStyleTableRecord;
			string empty = string.Empty;
			bool flag = false;
			bool flag2 = false;
			int num = 0;
			int num2 = 0;
			odDbTextStyleTableRecord.font(ref empty, out flag, out flag2, out num, out num2);
			bool flag3 = empty.StartsWith("@");
			if (flag3)
			{
				this.m_dFontRotation = Math.PI*.5;
			}
		}

		private void GetActualRotation()
		{
			bool flag = this.m_ptActualBoungding == null || this.m_ptActualBoungding.Count != 4;
			if (!flag)
			{
				OdGeVector3d odGeVector3d = this.m_ptActualBoungding[1] - this.m_ptActualBoungding[0];
				OdGeVector3d odGeVector3d2 = this.m_ptActualBoungding[0] - this.m_ptActualBoungding[2];
				OdGeTol tol = new OdGeTol();
				bool flag2 = odGeVector3d.isZeroLength(tol) && odGeVector3d2.isZeroLength(tol);
				if (!flag2)
				{
					OdGeVector3d odGeVector3d3 = odGeVector3d;
					bool flag3 = odGeVector3d3.isZeroLength(tol);
					if (flag3)
					{
						odGeVector3d3 = odGeVector3d2.rotateBy(-Math.PI*.5, OdGeVector3d.kZAxis);
					}
					double num = odGeVector3d3.angleTo(OdGeVector3d.kXAxis);
					bool flag4 = odGeVector3d3.y < 0.0;
					if (flag4)
					{
						num = Math.PI*2 - num;
					}
					this.m_ActualRotation = num;
				}
			}
		}

		private OdGePoint3d GetMTextActualLeftDownPos(OdDbMText odDbMText)
		{
			OdGePoint3d source = odDbMText.location();
			OdGePoint3d odGePoint3d = new OdGePoint3d(source);
			double num = odDbMText.actualHeight();
			double num2 = odDbMText.actualWidth();
			switch (odDbMText.attachment())
			{
			case OdDbMText.AttachmentPoint.kTopLeft:
				odGePoint3d.y -= num;
				break;
			case OdDbMText.AttachmentPoint.kTopCenter:
				odGePoint3d.x -= num2 / 2.0;
				odGePoint3d.y -= num;
				break;
			case OdDbMText.AttachmentPoint.kTopRight:
				odGePoint3d.x -= num2;
				odGePoint3d.y -= num;
				break;
			case OdDbMText.AttachmentPoint.kMiddleLeft:
				odGePoint3d.y -= num / 2.0;
				break;
			case OdDbMText.AttachmentPoint.kMiddleCenter:
				odGePoint3d.x -= num2 / 2.0;
				odGePoint3d.y -= num / 2.0;
				break;
			case OdDbMText.AttachmentPoint.kMiddleRight:
				odGePoint3d.x -= num2;
				odGePoint3d.y -= num / 2.0;
				break;
			case OdDbMText.AttachmentPoint.kBottomCenter:
				odGePoint3d.x -= num2 / 2.0;
				break;
			case OdDbMText.AttachmentPoint.kBottomRight:
				odGePoint3d.x -= num2;
				break;
			}
			return odGePoint3d;
		}

		private OdDbEntity m_odDbText = null;

		private OdGeMatrix3d m_matrix = null;

		private OdGePoint3d m_ptPos = null;

		private OdGeExtents3d m_geBounding = new OdGeExtents3d();

		private OdGePoint3dArray m_ptActualBoungding = new OdGePoint3dArray();

		private OdGePoint3d m_ptBoungdingCenter = null;

		private OdGePoint3d m_ptContentCenter = null;

		private double m_dHeight = 0.0;

		private double m_dWidth = 0.0;

		private double m_dFontRotation = 0.0;

		private double m_ActualRotation = 0.0;
	}
}
